import Router from 'koa-router'
import axios from 'axios'
// import Province from '../dbs/models/province'
// import Category from '../dbs/models/category'

let router = new Router({prefix: '/category'})

const sign = '2887e552643c295f7301096e12134e8d'

router.get('/crumbs', async ctx => {
  // let result = await Category.findOne({city: ctx.query.city.replace('市', '') || '北京'})
  // console.log(result)
  // if (result) {
  //   ctx.body = {
  //     areas: result.areas,
  //     types: result.types
  //   }
  // } else {
  //   ctx.body = {
  //     areas: [],
  //     types: []
  //   }
  // }

  let {status, data: {areas, types}} = await axios.get(`http://cp-tools.cn/categroy/crumbs`, {
    params: {
      sign,
      city: ctx.query.city.replace('市', '') || '北京'
    }
  })

  if (status === 200) {
    ctx.body = {
      areas,
      types
    }
  } else {
    ctx.body = {
      areas: [],
      types: []
    }
  }
})

export default router
